package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.ProductDetailsMapper;
import com.atguigu.spzx.manager.mapper.ProductMapper;
import com.atguigu.spzx.manager.mapper.ProductSkuMapper;
import com.atguigu.spzx.manager.service.ProductService;
import com.atguigu.spzx.model.entity.system.Product;
import com.atguigu.spzx.model.entity.system.ProductDetails;
import com.atguigu.spzx.model.entity.system.ProductSku;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;


    @Override
    public PageInfo<Product> findAll(Integer page, Integer limit, Product product) {
        PageHelper.startPage(page,limit);
        List<Product> list = productMapper.findAll(product);
        return new PageInfo<>(list);
    }

    @Override
    public void save(Product product) {
        //保存商品数据
        product.setStatus(0);//设置上架状态为0
        product.setAuditStatus(0);//设置审核状态为0
        productMapper.save(product);

        //保存sku数据
        List<ProductSku> productSkuList = product.getProductSkuList();

        for (int i = 0 ; i < productSkuList.size(); i++) {
            //获取productsku对象
            ProductSku productSku = productSkuList.get(i);
            productSku.setSkuCode(product.getId() + "_" + i);
            productSku.setProductId(product.getId());//设置商品id
            productSku.setSkuName(product.getName()+productSku.getSkuSpec());
            productSku.setSaleNum(0L);
            productSku.setStatus(0);
            productSkuMapper.save(productSku);//保存数据
        }

        //保存商品数据详情
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.save(productDetails);
    }

    @Override
    public void remove(Integer id) {
        productMapper.remove(id);
    }

    @Override
    public void updatestatus(Integer id, Integer status) {
        productMapper.updateStatus(id,status);
    }

    @Override
    public void updateAuditStatus(Integer id, Integer auditstatus) {
        if (id != null && auditstatus != null) {
            productMapper.updateAuditStatus(id, auditstatus);
        }
    }

    @Override
    @Transactional
    public void updateById(Product product) {
        //修改商品基本数据
        productMapper.updateById(product);

        //修改商品sku数据
        List<ProductSku> productSkuList = product.getProductSkuList();
        productSkuList.forEach(productSku -> {
            productSkuMapper.updateById(productSku);
        });
        //修改商品详情数据
        ProductDetails productDetails = productDetailsMapper.selectByProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.updateById(productDetails);
    }
}
